﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class AddNew : Form
    {
        public delegate void addDataHandler(Data data);
        public addDataHandler myAddData;
        public event addDataHandler addDataEvent;
        bool validatedx;
        bool validatedy;
        bool validatedl;
        int selectedColor;
        public AddNew()
        {
            InitializeComponent();
           // ColorControl c = new ColorControl();
            //c.Show();
            validatedx = false;
            validatedy = false;
            validatedl = false;
            colorControl1.ChangeColourEvent += new ColorControl.ChangeColourHandler(color);
        }
        void color(int col)
        {
            selectedColor = col;
            colorLabel.Text = col.ToString();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dodaj_Click(object sender, EventArgs e)
        {
            if (!(validatedx && validatedy && validatedl))
                return;
            if (valueLabel.Text.Length > 0 && valueX.Text.Length > 0 && valueY.Text.Length > 0)
            {
                Data d = new Data(valueLabel.Text, int.Parse(valueX.Text), int.Parse(valueY.Text), selectedColor);
                addDataEvent(d);
                this.Close();
            }
        }

        private void AddNew_Load(object sender, EventArgs e)
        {

        }

        private void valueX_Validating(object sender, CancelEventArgs e)
        {
            validatedx = false;
           int num;
            bool isnum = int.TryParse(valueX.Text, out num);
            if (isnum)
                errorProvider1.SetError(valueX, "");
            else
            {
                errorProvider1.SetError(valueX, "not numeric");
                e.Cancel = true;
            }
        }

        private void valueX_Validated(object sender, EventArgs e)
        {
            validatedx = true;
        }

        private void valueY_Validated(object sender, EventArgs e)
        {
            validatedy = true;
        }

        private void valueY_Validating(object sender, CancelEventArgs e)
        {
            validatedy = false;
            int num;
            bool isnum = int.TryParse(valueY.Text, out num);
            if (isnum)
                errorProvider1.SetError(valueY, "");
            else
            {
                errorProvider1.SetError(valueY, "not numeric");
                e.Cancel = true;
            }
        }

        private void valueLabel_Validating(object sender, CancelEventArgs e)
        {
            validatedl = false;
            if (valueLabel.Text.Length <= 0)
            {
                errorProvider1.SetError(valueLabel, "empty");
                e.Cancel = true;
            }
            else
            {
                errorProvider1.SetError(valueLabel, "");
            }
        }
        private void valueLabel_Validated(object sender, EventArgs e)
        {
            validatedl = true;
        }

        private void colorControl1_Load(object sender, EventArgs e)
        {
            colorLabel.Text = colorControl1.Color.ToString();
            selectedColor = colorControl1.Color;
        }
    }
}
